package com.lagou.sevice.impl;

import com.lagou.dao.AccountDao;
import com.lagou.dao.AccountDaoImpl;
import com.lagou.pojo.Account;
import com.lagou.sevice.TransferService;
import com.lagou.utils.ConnectionUtils;
import com.lagou.utils.TransactionManager;

import java.sql.SQLException;

public class TransferServiceImpl implements TransferService {

    private AccountDao accountDao = new AccountDaoImpl();

    TransactionManager transactionManager = new TransactionManager();
    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        try {
            //开启事务
            transactionManager.beginTransaction();
            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);
            accountDao.updateAccountByCardNo(to);
            int i=0/1;
            accountDao.updateAccountByCardNo(from);
            transactionManager.commit();
        }catch (Exception e){
            e.printStackTrace();
            //回滚
            transactionManager.rollback();
            throw e;
        }




    }
}
